package com.pc.scrs.dao;

import static com.pc.scrs.dao.CourseDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import com.pc.scrs.bean.po.Course;
import java.util.List;
import java.util.Optional;
import javax.annotation.Generated;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

@Mapper
public interface CourseDao {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    BasicColumn[] selectList = BasicColumn.columnList(id, title, teacherId, subjectId, lessonNum, cover, description, viewCount, status, createTime, updateTime, enable, required);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    long count(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @DeleteProvider(type=SqlProviderAdapter.class, method="delete")
    int delete(DeleteStatementProvider deleteStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @InsertProvider(type=SqlProviderAdapter.class, method="insert")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="record.id", before=false, resultType=Integer.class)
    int insert(InsertStatementProvider<Course> insertStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("CourseResult")
    Optional<Course> selectOne(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="CourseResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="teacher_id", property="teacherId", jdbcType=JdbcType.INTEGER),
        @Result(column="subject_id", property="subjectId", jdbcType=JdbcType.INTEGER),
        @Result(column="lesson_num", property="lessonNum", jdbcType=JdbcType.INTEGER),
        @Result(column="cover", property="cover", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="view_count", property="viewCount", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="enable", property="enable", jdbcType=JdbcType.INTEGER),
        @Result(column="required", property="required", jdbcType=JdbcType.INTEGER)
    })
    List<Course> selectMany(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @UpdateProvider(type=SqlProviderAdapter.class, method="update")
    int update(UpdateStatementProvider updateStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, course, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, course, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int deleteByPrimaryKey(Integer id_) {
        return delete(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insert(Course record) {
        return MyBatis3Utils.insert(this::insert, record, course, c ->
            c.map(title).toProperty("title")
            .map(teacherId).toProperty("teacherId")
            .map(subjectId).toProperty("subjectId")
            .map(lessonNum).toProperty("lessonNum")
            .map(cover).toProperty("cover")
            .map(description).toProperty("description")
            .map(viewCount).toProperty("viewCount")
            .map(status).toProperty("status")
            .map(createTime).toProperty("createTime")
            .map(updateTime).toProperty("updateTime")
            .map(enable).toProperty("enable")
            .map(required).toProperty("required")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertSelective(Course record) {
        return MyBatis3Utils.insert(this::insert, record, course, c ->
            c.map(title).toPropertyWhenPresent("title", record::getTitle)
            .map(teacherId).toPropertyWhenPresent("teacherId", record::getTeacherId)
            .map(subjectId).toPropertyWhenPresent("subjectId", record::getSubjectId)
            .map(lessonNum).toPropertyWhenPresent("lessonNum", record::getLessonNum)
            .map(cover).toPropertyWhenPresent("cover", record::getCover)
            .map(description).toPropertyWhenPresent("description", record::getDescription)
            .map(viewCount).toPropertyWhenPresent("viewCount", record::getViewCount)
            .map(status).toPropertyWhenPresent("status", record::getStatus)
            .map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime)
            .map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime)
            .map(enable).toPropertyWhenPresent("enable", record::getEnable)
            .map(required).toPropertyWhenPresent("required", record::getRequired)
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<Course> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, course, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<Course> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, course, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<Course> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, course, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<Course> selectByPrimaryKey(Integer id_) {
        return selectOne(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, course, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateAllColumns(Course record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(title).equalTo(record::getTitle)
                .set(teacherId).equalTo(record::getTeacherId)
                .set(subjectId).equalTo(record::getSubjectId)
                .set(lessonNum).equalTo(record::getLessonNum)
                .set(cover).equalTo(record::getCover)
                .set(description).equalTo(record::getDescription)
                .set(viewCount).equalTo(record::getViewCount)
                .set(status).equalTo(record::getStatus)
                .set(createTime).equalTo(record::getCreateTime)
                .set(updateTime).equalTo(record::getUpdateTime)
                .set(enable).equalTo(record::getEnable)
                .set(required).equalTo(record::getRequired);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateSelectiveColumns(Course record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(title).equalToWhenPresent(record::getTitle)
                .set(teacherId).equalToWhenPresent(record::getTeacherId)
                .set(subjectId).equalToWhenPresent(record::getSubjectId)
                .set(lessonNum).equalToWhenPresent(record::getLessonNum)
                .set(cover).equalToWhenPresent(record::getCover)
                .set(description).equalToWhenPresent(record::getDescription)
                .set(viewCount).equalToWhenPresent(record::getViewCount)
                .set(status).equalToWhenPresent(record::getStatus)
                .set(createTime).equalToWhenPresent(record::getCreateTime)
                .set(updateTime).equalToWhenPresent(record::getUpdateTime)
                .set(enable).equalToWhenPresent(record::getEnable)
                .set(required).equalToWhenPresent(record::getRequired);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKey(Course record) {
        return update(c ->
            c.set(title).equalTo(record::getTitle)
            .set(teacherId).equalTo(record::getTeacherId)
            .set(subjectId).equalTo(record::getSubjectId)
            .set(lessonNum).equalTo(record::getLessonNum)
            .set(cover).equalTo(record::getCover)
            .set(description).equalTo(record::getDescription)
            .set(viewCount).equalTo(record::getViewCount)
            .set(status).equalTo(record::getStatus)
            .set(createTime).equalTo(record::getCreateTime)
            .set(updateTime).equalTo(record::getUpdateTime)
            .set(enable).equalTo(record::getEnable)
            .set(required).equalTo(record::getRequired)
            .where(id, isEqualTo(record::getId))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKeySelective(Course record) {
        return update(c ->
            c.set(title).equalToWhenPresent(record::getTitle)
            .set(teacherId).equalToWhenPresent(record::getTeacherId)
            .set(subjectId).equalToWhenPresent(record::getSubjectId)
            .set(lessonNum).equalToWhenPresent(record::getLessonNum)
            .set(cover).equalToWhenPresent(record::getCover)
            .set(description).equalToWhenPresent(record::getDescription)
            .set(viewCount).equalToWhenPresent(record::getViewCount)
            .set(status).equalToWhenPresent(record::getStatus)
            .set(createTime).equalToWhenPresent(record::getCreateTime)
            .set(updateTime).equalToWhenPresent(record::getUpdateTime)
            .set(enable).equalToWhenPresent(record::getEnable)
            .set(required).equalToWhenPresent(record::getRequired)
            .where(id, isEqualTo(record::getId))
        );
    }

    @ResultMap("course_subject")
    List<Course> getCourseList(@Param("title") String title, @Param("subjectId") Integer subjectId, @Param("enable") Integer enable, @Param("required") Integer required);

    @Update("update edu_course set enable = 0 where id = #{id}")
    Integer disableCourse(Integer id);

    @Update("update edu_course set enable = 1 where id = #{id}")
    Integer enableCourse(Integer id);

    @Select("select id,title,cover,subject_id,lesson_num,description,status,required from edu_course where id = #{id}")
    Course queryCourseById(Integer id);

    @ResultMap("course_subject")
    List<Course> getRequiredList();

    @ResultMap("course_subject")
    List<Course> getSelectList();

    @ResultMap("course_subject")
    Course querySubjectTitleByCourseId(Integer courseId);

    @ResultMap("course_subject")
    List<Course> randomQueryCourseBySubjectTitle(String subjectTitle);

    @Insert("insert into rel_student_course(student_id,course_id) values(#{studentId},#{courseId})")
    Integer selectCourse(@Param("studentId")Integer studentId,@Param("courseId") Integer courseId);

    @ResultMap("course_student")
    Course selectedOrNot(@Param("studentId")Integer studentId,@Param("courseId") Integer courseId);

    @ResultMap("course_subject")
    List<Course> randomCourseList(List<Integer> ids);

    @ResultMap("course_subject_teacher")
    List<Course> mySelectList(Integer studentId);

    @Delete("delete from rel_student_course where student_id = #{studentId} and course_id = #{courseId}")
    Integer cancelCourse(@Param("studentId")Integer studentId,@Param("courseId") Integer courseId);

}
